package ${de.packageName}.service.Impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import ${de.packageName}.entity.${de.entityName};
import ${de.packageName}.mapper.${de.entityName}Mapper;
import ${de.packageName}.entity.vo.EasyUIResult;
import ${de.packageName}.service.BaseService;
import ${de.packageName}.service.${de.entityName}Service;
import ${de.packageName}.util.HdUtil;
import ${de.packageName}.entity.vo.PageParam;
import ${de.packageName}.entity.vo.ResultObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.util.*;


/**
 * ${de.tableComment}Service
 *
 * @author 
 * @date ${.now?date}
 */
@Service
public class ${de.entityName}ServiceImpl extends BaseService<${de.entityName}>  implements ${de.entityName}Service{
    Logger logger = LoggerFactory.getLogger(${de.entityName}Service.class);
    //@Autowired
    //private ${de.entityName}Mapper ${de.entityNameAlias}Mapper;
    

    /**
     * 查询${de.tableComment}
     *
     * @param params
     * @return
     * @author 
     * @date ${.now?date}
     */
    public ${de.entityName} get${de.entityName}(Map<String, Object> params) {
    	${de.entityName} ${de.entityNameAlias}=null;
    	Object ${de.keyColumn}=params.get("${de.keyColumn}");
    	if(${de.keyColumn}!=null){
    		Example example = new Example(${de.entityName}.class);
    		example.createCriteria().andEqualTo("${de.keyColumn}",${de.keyColumn});
        	List<${de.entityName}> ${de.entityName}List = this.selectByExample(example);
        	if (${de.entityName}List.isEmpty()) {
            	
       	 	} else {
            	${de.entityNameAlias}= ${de.entityName}List.get(0);
        	}
    	}
        return ${de.entityNameAlias};
    }




    

/**
     * @Description:获得所有实体列表，不分页
     * @Author:
     * @Date:
     * @param params
     * @return:
     **/
    public List<${de.entityName}> getAll${de.entityName}List(HashMap params) {
        List<${de.entityName}> result=null;   
        //params中有conditions参数，将conditions转化成Conditon的集合，用Example做
        //getExampleFromConditions在HdUtil里面
        Example example=HdUtil.getExampleFromConditions(${de.entityName}.class,params);
        result=this.selectByExample(example); 
        return result;
    }

    
    /**
     * 查询${de.tableComment}List
     *
     * @param params
     * @param pageParam
     * @return
     * @author 
     * @date ${.now?date}
     */
    public EasyUIResult get${de.entityName}List(HashMap params, PageParam pageParam) {
      Object isQuery=params.get("isQuery");
        //isQuery不存在，或者isQuery=true时，查询，否则不查询
      EasyUIResult result=new EasyUIResult();
      if(isQuery==null||isQuery.equals("true")){
          if(pageParam.getRows()==0){//查询全部
              List<${de.entityName}> list=getAll${de.entityName}List(params);
              result.setRows(list);
              result.setTotal(list.size());
              return result;
          }else{
              Page p = PageHelper.startPage(pageParam.getPage(), pageParam.getRows());
              //params中有conditions参数，将conditions转化成Conditon的集合，用Example做
              //getExampleFromConditions在HdUtil里面
              Example example=HdUtil.getExampleFromConditions(${de.entityName}.class,params);
              this.selectByExample(example);//执行这一步，就把查询的结果赋值给Page
              result.setRows(p.getResult());
              result.setTotal(p.getTotal());
          }
      }else{
          result.setTotal(0);
          result.setRows(new ArrayList());
      }
      return result;
    }

  


    
    /**
     * 保存${de.tableComment}
     * 
     * @param 
     * @return
     * @author 
     * @date ${.now?date}
     * 如果mybatis 仅仅try catch而不抛出异常，@Transactional注解无效，所以处理异常放到了controller
     */
    @Transactional
    public ResultObject save${de.entityName}(HashMap params)  throws Exception{
        ResultObject resultObject=null;     
        String saveEntity=params.get("saveEntity").toString();
        ${de.entityName} ${de.entityNameAlias}= JSON.parseObject(saveEntity,${de.entityName}.class);
        Object isAdd=params.get("isAdd");
        if(isAdd==null||isAdd.equals("true")){
            HdUtil.setCommonPro(true,${de.entityNameAlias});
            this.insertSelective(${de.entityNameAlias});
            //this.insertSelectiveAndLog(${de.entityNameAlias});
        }else{
            HdUtil.setCommonPro(false,${de.entityNameAlias});
            this.updateByPrimaryKeySelective(${de.entityNameAlias});
            //this.updateByPrimaryKeySelectiveAndLog(${de.entityNameAlias});
        }
        resultObject=new ResultObject(1,"保存${de.tableComment}成功");
        return resultObject;
    }

    /**
     * 删除${de.tableComment}
     *
     * @param 
     * @return 
     * @author 
     * @date ${.now?date}
     * 如果mybatis 仅仅try catch而不抛出异常，@Transactional注解无效，所以处理异常放到了controller
     */
    @Transactional
    public ResultObject delete${de.entityName}(HashMap params) throws Exception {
    	ResultObject resultObject=null;    
        String delstr=params.get("delstr").toString();
        List<String> delids= Arrays.asList(delstr.split(","));
        Example example=new Example(${de.entityName}.class);
        example.createCriteria().andIn("${de.keyColumn}",delids);
        mapper.deleteByExample(example);
        //this.deleteByExampleAndLog(example);
        resultObject=new ResultObject(1,"删除${de.tableComment}成功");    
        return resultObject;
    
    }

    
}
